#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSerialPortInfo>
#include "StepEnum.h"
#include <QSettings>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QInputDialog>
#include <QDateTime>
QByteArray globalSendData;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    SlotInit();
    InitPortCanUse();
    InitComboBox();
    box[0].slaveID=250;
    box[1].slaveID=3;
    box[2].slaveID=3;
    box[3].slaveID=3;
    box[4].slaveID=3;

    ReadConfigFile();
    QJsonObject TempObj;
    for(const QJsonValue& value :HallArray)
    {
        TempObj = value.toObject();
        ui->comboBox_11->addItem(TempObj.value("name").toString());
        WriteFlash.append(TempObj.value("name").toString());
    }
    PharseConfigFile();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::SlotInit()
{
    //切换stackWidget
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(ShowMainWidget()));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(ShowComSetWidget()));
    connect(ui->pushButton_33,SIGNAL(clicked()),this,SLOT(ShowParamSetWidget()));

    //打开或关闭串口
    connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(Slot_OpenCom1Device()));
    connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(Slot_CloseCom1Device()));
    connect(&hallDevice[0],SIGNAL(ToShowMessageBox(QString)),this,SLOT(Slot_ShowOpenComWidget(QString)));

    //向编程器发送调试信息
    connect(ui->pushButton_13,SIGNAL(clicked()),this,SLOT(SendTestData()));
    connect(ui->pushButton_14,SIGNAL(clicked()),this,SLOT(SwitchToReadWriteMode()));
    connect(ui->pushButton_15,SIGNAL(clicked()),this,SLOT(SwitchToMeasureMode()));
    connect(ui->pushButton_16,SIGNAL(clicked()),this,SLOT(WriteZeroDacData()));

    //box对象打开串口
    connect(ui->pushButton_18,SIGNAL(clicked()),this,SLOT(Slot_OpenBoxDevice1Com()));
    connect(ui->pushButton_17,SIGNAL(clicked()),this,SLOT(Slot_CloseBoxDevice1Com()));
    //box打开串口，成功与失败的弹窗信息
    connect(&box[0],SIGNAL(ShowOpenComWidget(QString)),this,SLOT(Slot_ShowOpenComWidget(QString)));
    //box对象读取电压信息
    connect(ui->pushButton_19,SIGNAL(clicked()),this,SLOT(Slot_Box1ReadVoltage()));

    //可编程转接采集盒2
    connect(ui->pushButton_21,SIGNAL(clicked()),this,SLOT(Slot_OpenBoxDevice2Com()));
    connect(ui->pushButton_20,SIGNAL(clicked()),this,SLOT(Slot_CloseBoxDevice2Com()));
    connect(ui->pushButton_28,SIGNAL(clicked()),this,SLOT(Slot_Box2ReadVoltage()));
    connect(&box[1],SIGNAL(ShowOpenComWidget(QString)),this,SLOT(Slot_ShowOpenComWidget(QString)));

    //可编程转接采集盒3
    connect(ui->pushButton_23,SIGNAL(clicked()),this,SLOT(Slot_OpenBoxDevice3Com()));
    connect(ui->pushButton_22,SIGNAL(clicked()),this,SLOT(Slot_CloseBoxDevice3Com()));
    connect(ui->pushButton_29,SIGNAL(clicked()),this,SLOT(Slot_Box3ReadVoltage()));
    connect(&box[2],SIGNAL(ShowOpenComWidget(QString)),this,SLOT(Slot_ShowOpenComWidget(QString)));

    //可编程转接采集盒4
    connect(ui->pushButton_25,SIGNAL(clicked()),this,SLOT(Slot_OpenBoxDevice4Com()));
    connect(ui->pushButton_24,SIGNAL(clicked()),this,SLOT(Slot_CloseBoxDevice4Com()));
    connect(ui->pushButton_30,SIGNAL(clicked()),this,SLOT(Slot_Box4ReadVoltage()));
    connect(&box[3],SIGNAL(ShowOpenComWidget(QString)),this,SLOT(Slot_ShowOpenComWidget(QString)));

    //可编程转接采集盒5
    connect(ui->pushButton_27,SIGNAL(clicked()),this,SLOT(Slot_OpenBoxDevice5Com()));
    connect(ui->pushButton_26,SIGNAL(clicked()),this,SLOT(Slot_CloseBoxDevice5Com()));
    connect(ui->pushButton_31,SIGNAL(clicked()),this,SLOT(Slot_Box5ReadVoltage()));
    connect(&box[4],SIGNAL(ShowOpenComWidget(QString)),this,SLOT(Slot_ShowOpenComWidget(QString)));
    //开始执行标定
    connect(ui->pushButton_32,SIGNAL(clicked()),this,SLOT(Slot_StartAutoTest()));
    //执行具体步骤的timer
    connect(&ProcessEngine,SIGNAL(timeout()),this,SLOT(Slot_AutoTest()));


    //添加产品型号
    connect(ui->pushButton_34,SIGNAL(clicked()),this,SLOT(LoadHallConfig()));
    connect(ui->pushButton_36,SIGNAL(clicked()),this,SLOT(AddProductModel()));
    connect(ui->pushButton_37,SIGNAL(clicked()),this,SLOT(SaveHallConfig()));

    //显示收发数据
    connect(&hallDevice[0],SIGNAL(ToShowSendRecv(QString)),this,SLOT(Slot_ShowString(QString)));
    connect(ui->pushButton_35,SIGNAL(clicked()),this,SLOT(HandleWrite()));

    //烧写
    connect(ui->pushButton_38,SIGNAL(clicked()),this,SLOT(BurnAll()));

    //打开恒流源串口
    connect(ui->pushButton_40,SIGNAL(clicked()),this,SLOT(Slot_OpenCurrentSource()));
    connect(ui->pushButton_41,SIGNAL(clicked()),this,SLOT(Slot_CloseCurrentSource()));
    connect(ui->pushButton_39,SIGNAL(clicked()),this,SLOT(Slot_ShowCurrentSourceWidget()));
    connect(ui->pushButton_42,SIGNAL(clicked()),this,SLOT(Slot_0A()));
    connect(ui->pushButton_43,SIGNAL(clicked()),this,SLOT(Slot_50A()));
    connect(&currentSource,SIGNAL(ToShowSendRecvData(QString)),this,SLOT(Slot_ShowString(QString)));
    connect(&currentSource,SIGNAL(ToShowOpenDeviceStatus(QString)),this,SLOT(Slot_ShowOpenCurrent(QString)));

}

void MainWindow::InitComboBox()
{
    m_G0<<"2.4"<<"2.8"<<"3.2"<<"3.6";
    m_G1<<"1"<<"2"<<"4"<<"8"<<"16"<<"32"<<"64"<<"128";
    m_G2<<"1.182"<<"1.4"<<"1.667"<<"2"<<"2.429"<<"3"<<"3.5"<<"4";
    m_EXC<<"3mA"<<"1.5mA"<<"2mA"<<"2.5mA"<<"3.5mA"<<"4mA"<<"4.5mA"<<"5mA"<<"6mA"<<"7mA"<<"8mA"<<"9mA"<<"10mA";
    m_VEXC<<"4.096V"<<"2.5V";                       //OK
    m_SWAP<<"正负不交换"<<"正负极交换";                //OK
    m_VBIAS<<"2.5V"<<"1.65V"<<"0.5V"<<"1/2VCC";     //OK
    m_Bandwidth<<"600kHz"<<"250kHz"<<"500kHz"<<"50kHz";//OK

    ui->comboBox_G0->addItems(m_G0);
    ui->comboBox_G1->addItems(m_G1);
    ui->comboBox_G2->addItems(m_G2);
    ui->comboBox_EXC->addItems(m_EXC);
    ui->comboBox_VEXC->addItems(m_VEXC);
    ui->comboBox_SWAP->addItems(m_SWAP);
    ui->comboBox_VBIAS->addItems(m_VBIAS);
    ui->comboBox_Bandwidth->addItems(m_Bandwidth);
    //m_WriteFlash<<"霍尔name1"<<"霍尔name2"<<"霍尔name3"<<"霍尔name4"<<"霍尔name5";
    ui->lineEdit_ExpectUn->setText("4.5");
    ui->lineEdit_InternalRefZero->setText("2.5");
}

void MainWindow::InitPortCanUse()
{
    for(QSerialPortInfo x:QSerialPortInfo::availablePorts())
    {
        ui->comboBox->addItem(x.portName());
        ui->comboBox_2->addItem(x.portName());
        ui->comboBox_3->addItem(x.portName());
        ui->comboBox_4->addItem(x.portName());
        ui->comboBox_5->addItem(x.portName());
        ui->comboBox_6->addItem(x.portName());

        ui->comboBox_7->addItem(x.portName());
        ui->comboBox_8->addItem(x.portName());
        ui->comboBox_9->addItem(x.portName());
        ui->comboBox_10->addItem(x.portName());

        ui->comboBox_12->addItem(x.portName());
    }
}

QByteArray MainWindow::PackageSendData(int messageType)
{
    QByteArray PackageData;
    PackageData.clear();
    if(messageType==0)
    {
        QString message="010307d10001d547";
        PackageData=QByteArray::fromHex(message.toUtf8());
    }

    return PackageData;
}

QByteArray MainWindow::RecvProgrammingDeviceData(HallDevice* hDevice)
{
    while (!hDevice->hasRecv)
    {
        QApplication::processEvents();
    }
    return hDevice->recvBuf;
}

void MainWindow::ShowMainWidget()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::ShowComSetWidget()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::ShowParamSetWidget()
{
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::Slot_OpenCom1Device()
{
    qDebug()<<"打开端口com1";
    hallDevice[0].serialCoreObject->mPort.setPortName(ui->comboBox->currentText());
    hallDevice[0].serialCoreObject->mPort.setBaudRate(9600);
    hallDevice[0].serialCoreObject->mPort.setDataBits(QSerialPort::Data8);
    hallDevice[0].serialCoreObject->mPort.setStopBits(QSerialPort::OneStop);
    hallDevice[0].serialCoreObject->mPort.setParity(QSerialPort::NoParity);
    hallDevice[0].OpenDevice();
}

void MainWindow::Slot_CloseCom1Device()
{
    qDebug()<<"关闭端口com1";
    hallDevice[0].CloseDevice();
}

void MainWindow::SendTestData()
{
    //hallDevice[0].HallChipControl(01);
    hallDevice[0].HallChipControl(5);
    QThread::msleep(100);
    hallDevice[0].HallChipControl(3);
    QThread::msleep(100);
    hallDevice[0].HallChipControl(1);
    //hallDevice[0].ChannelSwitch(0);
}

void MainWindow::SwitchToReadWriteMode()
{
    qDebug()<<__FUNCTION__;
    hallDevice[0].ChannelSwitch(1);
}
//调到测试模式
void MainWindow::SwitchToMeasureMode()
{
    hallDevice[0].WriteSingleRegister(0x22,1);
    QThread::msleep(100);
    hallDevice[0].WriteSingleRegister(0x21,255);
    QThread::msleep(100);
    hallDevice[0].WriteSingleRegister(0x20,1);
    QThread::msleep(100);
    hallDevice[0].ChannelSwitch(0);
}

void MainWindow::WriteZeroDacData()
{
    hallDevice[0].WriteSingleRegister(0x10,1);


    hallDevice[0].WriteSingleRegister(8,(ui->spinBox->value()>>8)&0x3f);
    hallDevice[0].WriteSingleRegister(9,ui->spinBox->value()&0xff);
    qDebug()<<ui->spinBox->value();
}
void MainWindow::Slot_OpenBoxDevice1Com()
{
    box[0].modBusDevice->port.setPortName(ui->comboBox_6->currentText());
    box[0].modBusDevice->port.setBaudRate(9600);
    box[0].modBusDevice->port.setDataBits(QSerialPort::Data8);
    box[0].modBusDevice->port.setParity(QSerialPort::NoParity);
    box[0].modBusDevice->port.setStopBits(QSerialPort::OneStop);
    box[0].OpenDevice();
}

void MainWindow::Slot_ShowOpenComWidget(QString text)
{
    QMessageBox a;
    a.setText(text);
    a.show();
    a.exec();
}

void MainWindow::Slot_CloseBoxDevice1Com()
{
    box[0].CloseDevice();
}

void MainWindow::Slot_Box1ReadVoltage()
{
    box[0].hasRecv=false;
    box[0].ReadVoltageInformation();
    float value=box[0].GetRecvVoltageValue();
    if(value==-1)
    {

    }
    else
    {
        ui->lineEdit->setText(QString::number(value));
    }
}

void MainWindow::Slot_OpenBoxDevice2Com()
{
    CollectingBox *boxTempPtr=&box[1];

    boxTempPtr->modBusDevice->port.setPortName(ui->comboBox_7->currentText());
    boxTempPtr->modBusDevice->port.setBaudRate(9600);
    boxTempPtr->modBusDevice->port.setDataBits(QSerialPort::Data8);
    boxTempPtr->modBusDevice->port.setParity(QSerialPort::NoParity);
    boxTempPtr->modBusDevice->port.setStopBits(QSerialPort::OneStop);
    boxTempPtr->OpenDevice();
}

void MainWindow::Slot_CloseBoxDevice2Com()
{
    box[1].CloseDevice();
}

void MainWindow::Slot_Box2ReadVoltage()
{
    box[1].hasRecv=false;
    box[1].ReadVoltageInformation();
    float value=box[1].GetRecvVoltageValue();
    if(value==-1)
    {

    }
    else
    {
        ui->lineEdit_2->setText(QString::number(value));
    }
}

void MainWindow::Slot_OpenBoxDevice3Com()
{
    CollectingBox *boxTempPtr=&box[2];

    boxTempPtr->modBusDevice->port.setPortName(ui->comboBox_8->currentText());
    boxTempPtr->modBusDevice->port.setBaudRate(9600);
    boxTempPtr->modBusDevice->port.setDataBits(QSerialPort::Data8);
    boxTempPtr->modBusDevice->port.setParity(QSerialPort::NoParity);
    boxTempPtr->modBusDevice->port.setStopBits(QSerialPort::OneStop);
    boxTempPtr->OpenDevice();
}

void MainWindow::Slot_CloseBoxDevice3Com()
{
    box[2].CloseDevice();
}

void MainWindow::Slot_Box3ReadVoltage()
{
    box[2].hasRecv=false;
    box[2].ReadVoltageInformation();
    float value=box[2].GetRecvVoltageValue();
    if(value==-1)
    {

    }
    else
    {
        ui->lineEdit_3->setText(QString::number(value));
    }
}

void MainWindow::Slot_OpenBoxDevice4Com()
{
    CollectingBox *boxTempPtr=&box[3];

    boxTempPtr->modBusDevice->port.setPortName(ui->comboBox_9->currentText());
    boxTempPtr->modBusDevice->port.setBaudRate(9600);
    boxTempPtr->modBusDevice->port.setDataBits(QSerialPort::Data8);
    boxTempPtr->modBusDevice->port.setParity(QSerialPort::NoParity);
    boxTempPtr->modBusDevice->port.setStopBits(QSerialPort::OneStop);
    boxTempPtr->OpenDevice();
}

void MainWindow::Slot_CloseBoxDevice4Com()
{
    box[3].CloseDevice();
}

void MainWindow::Slot_Box4ReadVoltage()
{
    box[3].hasRecv=false;
    box[3].ReadVoltageInformation();
    float value=box[3].GetRecvVoltageValue();
    if(value==-1)
    {

    }
    else
    {
        ui->lineEdit_4->setText(QString::number(value));
    }
}

void MainWindow::Slot_OpenBoxDevice5Com()
{
    CollectingBox *boxTempPtr=&box[4];

    boxTempPtr->modBusDevice->port.setPortName(ui->comboBox_10->currentText());
    boxTempPtr->modBusDevice->port.setBaudRate(9600);
    boxTempPtr->modBusDevice->port.setDataBits(QSerialPort::Data8);
    boxTempPtr->modBusDevice->port.setParity(QSerialPort::NoParity);
    boxTempPtr->modBusDevice->port.setStopBits(QSerialPort::OneStop);
    boxTempPtr->OpenDevice();
}

void MainWindow::Slot_CloseBoxDevice5Com()
{
    box[4].CloseDevice();
}

void MainWindow::Slot_Box5ReadVoltage()
{
    box[4].hasRecv=false;
    box[4].ReadVoltageInformation();
    float value=box[4].GetRecvVoltageValue();
    if(value==-1)
    {

    }
    else
    {
        ui->lineEdit_5->setText(QString::number(value));
    }
}

void MainWindow::Slot_OpenCurrentSource()
{
    qDebug()<<__FUNCTION__;
    currentSource.serialCoreObject->mPort.setPortName(ui->comboBox_12->currentText());
    currentSource.serialCoreObject->mPort.setBaudRate(4800);
    currentSource.serialCoreObject->mPort.setParity(QSerialPort::NoParity);
    currentSource.serialCoreObject->mPort.setDataBits(QSerialPort::Data8);
    currentSource.serialCoreObject->mPort.setStopBits(QSerialPort::OneStop);
    currentSource.OpenDevice();
}

void MainWindow::Slot_CloseCurrentSource()
{
    qDebug()<<__FUNCTION__;
    currentSource.CloseDevice();
}

void MainWindow::Slot_ShowCurrentSourceWidget()
{
    ui->stackedWidget->setCurrentIndex(3);
}

void MainWindow::Slot_0A()
{
    currentSource.Send0A();
}

void MainWindow::Slot_50A()
{
    currentSource.Send50A();
}

void MainWindow::Slot_ShowOpenCurrent(QString value)
{
    QMessageBox a;
    a.setText(value);
    a.show();
    a.exec();
}

void MainWindow::ReadConfigFile()
{

    QFile file("./霍尔配置参数.json");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream stream(&file);
        QString jsonString = stream.readAll();
        file.close();
        QJsonDocument jsonDoc   = QJsonDocument::fromJson(jsonString.toUtf8());
        QJsonObject HallObj     = jsonDoc.object();
        HallArray               = HallObj["霍尔类型"].toArray();
    }
}

void MainWindow::PharseConfigFile()
{
    QJsonObject TempObj;
    float ftempData;
    TempObj = HallArray[ui->comboBox_11->currentIndex()].toObject();

    ui->comboBox_G0->setCurrentIndex(TempObj["Go"].toString().toInt());
    //G1
    ui->comboBox_G1->setCurrentIndex(TempObj["G1"].toString().toInt());
    //G2
    ui->comboBox_G2->setCurrentIndex(TempObj["G2"].toString().toInt());

    ui->comboBox_EXC->setCurrentIndex(TempObj["EXC"].toString().toInt());
    //SWAP
    ui->comboBox_SWAP->setCurrentIndex(TempObj["SWAP"].toString().toInt());
    //VEXC
    ui->comboBox_VEXC->setCurrentIndex(TempObj["VEXC"].toString().toInt());

    ui->comboBox_VBIAS->setCurrentIndex(TempObj["VBIAS"].toString().toInt());
    //Bandwidth
    ui->comboBox_Bandwidth->setCurrentIndex(TempObj["Bandwidth"].toString().toInt());

    ui->TC1_SpinBox->setValue(TempObj["TC1"].toString().toInt());
    ui->TC2_SpinBox->setValue(TempObj["TC2"].toString().toInt());

    ui->lineEdit_ExpectUn->setText(QString("%1").arg(TempObj["ExpectUn"].toString().toDouble()));
    //InternalRefZero
    ui->lineEdit_InternalRefZero->setText(QString("%1").arg(TempObj["ExpectZero"].toString().toDouble()));
    //ActualRefZero
    ui->lineEdit_ActualRefZero->setText(QString("%1").arg(TempObj["ChaFenZero"].toString().toDouble()));

    ftempData = TempObj["CalibError"].toString().toDouble();
    if(ftempData <= 0)
    {
        ftempData = 1;
    }    //校准误差不超过1mv为默认值
    ui->lineEdit_CalibError->setText(QString("%1").arg(ftempData));

    for(int i=0;i<=4;i++)
    {
        hallDevice[i].Go_Gain = ui->comboBox_G0->currentText().toFloat();
        hallDevice[i].G1_Gain = ui->comboBox_G1->currentText().toFloat();
        hallDevice[i].G2_Gain = ui->comboBox_G2->currentText().toFloat();
        hallDevice[i]. GS302_VEXC = ui->comboBox_VEXC->currentText().toFloat();

        hallDevice[i].ZeroDAC=TempObj["Zero"].toString().toDouble();
        hallDevice[i].GS302_ZeroDAC = hallDevice[i].ZeroDAC;
        ui->label_FloatZero->setText(QString("%1").arg(hallDevice[i].ZeroDAC));

        hallDevice[i].GainDAC = TempObj["Gain"].toString().toDouble();
        hallDevice[i].GS302_GainDAC = hallDevice[i].GainDAC*1.5-0.5;
        ui->label_FloatGain->setText(QString("%1").arg(hallDevice[i].GainDAC));
    }
}

void MainWindow::GetWriteInBufferValue()
{
     uint16_t TempData;
    for(int i=0;i<=4;i++)
    {
        //VEXC 0x31
        hallDevice[i].GS302_R31   = 0;
        hallDevice[i].GS302_VBIAS = 0;
        hallDevice[i].GS302_GG0   = 0;
        hallDevice[i].GS302_R30   = 0;
        switch (ui->comboBox_EXC->currentIndex()) {
        //电流激励
        case 0:hallDevice[i].GS302_R31 |= 0x30; break;          //激励电流 3mA
        case 1:hallDevice[i].GS302_R31 &= 0x0F; break;          //激励电流 1.5mA
        case 2:hallDevice[i].GS302_R31 |= 0x10; break;          //激励电流 2mA
        case 3:hallDevice[i].GS302_R31 |= 0x20; break;          //激励电流 2.5mA
        case 4:hallDevice[i].GS302_R31 |= 0x40; break;          //激励电流 3.5mA
        case 5:hallDevice[i].GS302_R31 |= 0x50; break;          //激励电流 4mA
        case 6:hallDevice[i].GS302_R31 |= 0x60; break;          //激励电流 4.5mA
        case 7:hallDevice[i].GS302_R31 |= 0x70; break;          //激励电流 5mA
        case 8:hallDevice[i].GS302_R31 |= 0xB0; break;          //激励电流 6mA
        case 9:hallDevice[i].GS302_R31 |= 0xC0; break;          //激励电流 7mA
        case 10:hallDevice[i].GS302_R31 |= 0xD0; break;         //激励电流 8mA
        case 11:hallDevice[i].GS302_R31 |= 0xE0; break;         //激励电流 9mA
        case 12:hallDevice[i].GS302_R31 |= 0xF0; break;         //激励电流 10mA
        default:break;
        }

        switch (ui->comboBox_VEXC->currentIndex()) {
        //零点偏置电压
        case 0:hallDevice[i].GS302_R31 &= 0xF7;
            hallDevice[i].GS302_VEXC = 4.096;          break;          //4.096V
        case 1:hallDevice[i].GS302_R31 |= 0x08;
            hallDevice[i].GS302_VEXC = 2.5;            break;          //2.5V
        default:break;
        }
        switch (ui->comboBox_SWAP->currentIndex()) {
        //输出方向
        case 0:hallDevice[i].GS302_R31 &= 0xFB; break;          //正负不交换
        case 1:hallDevice[i].GS302_R31 |= 0x04; break;          //正负极交换
        default:break;
        }
        //VBIAS 0X41|0X43

        switch (ui->comboBox_Bandwidth->currentIndex())
        {
        //展波宽度
        case 0:
            hallDevice[i].GS302_VBIAS &= 0xE7;
            break;          //None
        case 1:
            hallDevice[i].GS302_VBIAS |= 0x10;
            break;          //250Hz
        case 2:
            hallDevice[i].GS302_VBIAS |= 0x08;
            break;          //500Hz
        case 3:
            hallDevice[i].GS302_VBIAS |= 0x18;
            break;          //50Hz
        default:break;
        }

        switch (ui->comboBox_VBIAS->currentIndex())
        {
        //外部参考电压
        case 0:hallDevice[i].GS302_VBIAS &= 0xF9; break;          //2.5V
        case 1:hallDevice[i].GS302_VBIAS |= 0x02; break;          //1.65V
        case 2:hallDevice[i].GS302_VBIAS |= 0x04; break;          //0.5V
        case 3:hallDevice[i].GS302_VBIAS |= 0x06; break;          //1/2VCC
        default:break;
        }

        /*ComGG0位配置*/
       // ui->textEdit->append(QString("go=%0,g1=%1,g2=%2").arg(ui->comboBox_G0->currentIndex()).arg(ui->comboBox_G1->currentIndex()).arg(ui->comboBox_G2->currentIndex()));
        switch (ui->comboBox_G0->currentIndex()) {
        //三阶增益Go
        case 0:hallDevice[i].GS302_GG0 &= 0x3F; break;          //2.4
        case 1:hallDevice[i].GS302_GG0 |= 0x40; break;          //2.8
        case 2:hallDevice[i].GS302_GG0 |= 0x80; break;          //3.2
        case 3:hallDevice[i].GS302_GG0 |= 0xC0; break;          //3.6
        default:break;
        }
        switch (ui->comboBox_G1->currentIndex()) {
        //一阶增益G1
        case 0:hallDevice[i].GS302_GG0 &= 0xF8; break;          //1
        case 1:hallDevice[i].GS302_GG0 |= 0x01; break;          //2
        case 2:hallDevice[i].GS302_GG0 |= 0x02; break;          //4
        case 3:hallDevice[i].GS302_GG0 |= 0x03; break;          //8
        case 4:hallDevice[i].GS302_GG0 |= 0x04; break;          //16
        case 5:hallDevice[i].GS302_GG0 |= 0x05; break;          //32
        case 6:hallDevice[i].GS302_GG0 |= 0x06; break;          //64
        case 7:hallDevice[i].GS302_GG0 |= 0x07; break;          //128
        default:break;
        }
        switch (ui->comboBox_G2->currentIndex()) {
        //二阶增益G2
        case 0:hallDevice[i].GS302_GG0 &= 0xC7; break;          //1.182
        case 1:hallDevice[i].GS302_GG0 |= 0x08; break;          //1.4
        case 2:hallDevice[i].GS302_GG0 |= 0x10; break;          //1.667
        case 3:hallDevice[i].GS302_GG0 |= 0x18; break;          //2
        case 4:hallDevice[i].GS302_GG0 |= 0x20; break;          //2.429
        case 5:hallDevice[i].GS302_GG0 |= 0x28; break;          //3
        case 6:hallDevice[i].GS302_GG0 |= 0x30; break;          //3.5
        case 7:hallDevice[i].GS302_GG0 |= 0x38; break;          //4
        default:break;
        }

        //hallDevice[i].SlaveAdrss      = 0xE1;                       //从机地址
        hallDevice[i].GS302_R31      |= 0x02;//自稳
        hallDevice[i].GS302_VBIAS    &= 0x1E;//高3位和最低位置0
        hallDevice[i].GS302_TC1 = ui->TC1_SpinBox->value();         //温度1补偿系数
        hallDevice[i].GS302_TC2 = ui->TC2_SpinBox->value();         //温度2补偿系数
        //加载零点下发参数

        TempData = hallDevice[i].GS302_ZeroDAC*16383;
        hallDevice[i].GS302_K0H = TempData>>8;
        hallDevice[i].GS302_K0L = TempData&0xFF;
        //加载灵敏度下发参数
        TempData = hallDevice[i].GS302_GainDAC*16383;
        hallDevice[i].GS302_KxH = TempData>>8;
        hallDevice[i].GS302_KxL = TempData&0xFF;

        ui->textEdit->append(QString("zeroDac=%0,hi=%1,low=%2,乘积=%3").arg(hallDevice[i].GS302_ZeroDAC).arg( hallDevice[i].GS302_K0H).arg(hallDevice[i].GS302_K0L).arg(hallDevice[i].GS302_ZeroDAC*16383));
    }

}
/************************************************************
 *
 *当当前的5个设备执行完操作之后，由此接口判断下一个步骤是什么
 *具体的逻辑是只要有一个设备执行成功就要到下一步，若5个设备都执行失败则停止
 *
 * ***********************************************************/
int MainWindow::GetNextStep()
{
    if(returnValue[0].isSuccess==false&&returnValue[1].isSuccess==false&&returnValue[2].isSuccess==false&&returnValue[3].isSuccess==false&&returnValue[4].isSuccess==false)
    {
        return ProcessEnd;
    }
    else
    {
        if(returnValue[0].isSuccess)
        {
            return returnValue[0].step;
            qDebug()<<"----->"<<__FUNCTION__;
        }
        if(returnValue[1].isSuccess)
        {
            return returnValue[1].step;
        }
        if(returnValue[2].isSuccess)
        {
            return returnValue[2].step;
        }
        if(returnValue[3].isSuccess)
        {
            return returnValue[3].step;
        }
        if(returnValue[4].isSuccess)
        {
            return returnValue[4].step;
        }
    }
    return ProcessEnd;
}

void MainWindow::Slot_AutoTest()
{
    qDebug()<<"current step is"<<nowStep;
    switch (nowStep)
    {

    case ProcessEnd:
        ui->textEdit->append("流程结束");
        ProcessEngine.stop();
        break;
    case PowerOn://控制芯片上电
        PowerOnChip(0);
        nowStep=GetNextStep();
        break;

    case ReadTADC://读TADC两个字节
        ReadTADCFuntion(0);
        nowStep=GetNextStep();
        break;
    case ReadBurnTimes://读烧写次数，确定一些变量的值
        ReadBurnTime(0);
        nowStep=GetNextStep();

        break;
    case StepToWriteMod://进入连续写模式
        StepChipToWriteMode(0);
        nowStep=GetNextStep();
        break;
    case WriteTC1://写TC1
        ChipWriteTC1(0);
        nowStep=GetNextStep();
        break;
    case WriteTC2://写TC2
        ChipWriteTC2(0);
        nowStep=GetNextStep();
        break;
    case StartCalibration://进入调试模式
        StartChipCalibration(0);
        nowStep=GetNextStep();
        break;
    case WriteBurnsNumber://写烧写次数【已对】
        ChipWriteBurnsNumber(0);
        nowStep=GetNextStep();
        break;
    case WriteVBIAS:      //写VBIAS
        ChipWriteVBIAS(0);
        nowStep=GetNextStep();
        break;
    case WriteVEXC:         //写Vexc
        ChipWriteVEXC(0);
        nowStep=GetNextStep();
        break;
    case WriteEnlarge://go g1 g2
        ChipWriteEnlarge(0);
        nowStep=GetNextStep();
        break;
    case WriteZeroL:
        ChipWriteZeroL(0);
        nowStep=GetNextStep();
        break;
    case WriteZeroH:
        ChipWriteZeroH(0);
        nowStep=GetNextStep();
        break;
    case WriteGainL:
        ChipWriteGainL(0);
        nowStep=GetNextStep();
        break;
    case WriteGainH:
        ChipWriteGainH(0);
        nowStep=GetNextStep();
        break;
    case SignalPowerOn:
        SourcePowerOn();
        nowStep=GetNextStep();
        break;
 /******************************开始上电*******************************/
    case  CaptureVoltage:           //上电后获取电压
        ChipCaptureVoltage(0);
        nowStep=GetNextStep();

        break;
    case GetGain:                   //粗调获取gain
        ChipGetGain(0);
        nowStep=GetNextStep();
        break;
    case Auto_RoughToWrite:         //写入粗条的G1 G2 GO
        ChipRoughToWrite(0);
        //HandleWrite();
        nowStep=GetNextStep();
        break;
    case WriteRoughZeroL:
        ChipRoughWriteZeroL(0); //写入粗条的Zero L
        nowStep=GetNextStep();
        break;
    case WriteRoughZeroH:       //写入粗条的Zero H
        ChipRoughWriteZeroH(0);
        nowStep=GetNextStep();
        break;
    case WriteRoughGainL:           //写入粗调的Gain L
        ChipRoughWriteGainL(0);
        nowStep=GetNextStep();
        break;
    case WriteRoughGainH:           //写入粗调的Gain H
        ChipRoughWriteGainH(0);
        nowStep=GetNextStep();
        break;
        //得到写入配置后的满值电压
    case CaptureAfterRoughVoltage:      //获得电压
        ChipCaptureAfterRoughVoltage(0);
        nowStep=GetNextStep();
        break;
    case SignalPowerOff:            //提示下电
        SourcePowerOff();
        nowStep=GetNextStep();
        break;
    case CaptureAfterPoweroffVoltage:       //获得电压
        ChipCaptureAfterPoweroffVoltage(0);
        nowStep=GetNextStep();
        break;
    case Auto_FineGetUnError:               //获得量程误差
        ChipGetUnError(0);
        nowStep=GetNextStep();
        break;
    case Auto_FineGetGain:                  //获得新的gain值
        ChipFineGetGain(0);
        nowStep=GetNextStep();
        break;
    case Auto_FineGainL:                    //写入gainL
        ChipFineWriteGainL(0);
        nowStep=GetNextStep();
        break;
    case Auto_FineGainH:                    //写入gainH
        ChipFineWriteGainH(0);
        nowStep=GetNextStep();
        break;
    case Auto_LookOutStart:                 //获得电压
        NormalGetVoltage(0);
        nowStep=GetNextStep();
        break;
        //计算当前零点误差
    case Auto_FineGetZeroError:             //计算零点误差
        ChipFineGetZeroError(0);
        nowStep=GetNextStep();
        break;
        //判断量程在不在范围内，如果不在获取新的zeroDac
    case Auto_FineGetZero:
        ChipFineGetZero(0);
        nowStep=GetNextStep();
        break;
        //写入ZeroL
    case Auto_FineZeroL:
        ChipFineWriteZeroL(0);
        nowStep=GetNextStep();
        break;
        //写入ZeroH
    case Auto_FineZeroH:
        ChipFineWriteZeroH(0);
        nowStep=GetNextStep();
        break;
        //计算零点误差
    case Auto_FineAloneZero1:
        ChipFineAloneZero1(0);
        nowStep=GetNextStep();
        break;
        //判断 否则写入Zero L zero H
    case Auto_FineAloneZero3:
        ChipFineAloneZero3(0);
        nowStep=GetNextStep();
        break;
        //加载量得的电压并计算出差值，转至3判断
    case Auto_FineAloneZero2:
        ChipFineAloneZero2(0);
        nowStep=GetNextStep();
        break;

        //********************************烧写***********************************
    case Auto_BurnStart:
        hallDevice[0].WriteSingleRegister(hallDevice[0].WReg_GainDACL,hallDevice[0].W_GainDACL);
        nowStep=Auto_BurnGainDACH;
        break;
    case Auto_BurnGainDACH:
        hallDevice[0].WriteSingleRegister(hallDevice[0].WReg_GainDACH,hallDevice[0].W_GainDACH);
        nowStep=Auto_BurnZeroDACL;
        break;
    case Auto_BurnZeroDACL:
        hallDevice[0].WriteSingleRegister(hallDevice[0].WReg_ZeroDACL,hallDevice[0].W_ZeroDACL);
        nowStep=Auto_BurnZeroDACH;
        break;
    case Auto_BurnZeroDACH:
        hallDevice[0].WriteSingleRegister(hallDevice[0].WReg_ZeroDACH,hallDevice[0].W_ZeroDACH);
        nowStep=Auto_BurnTADC;
        break;
    case Auto_BurnTADC:
        hallDevice[0].WriteSingleRegister(hallDevice[0].WReg_TADC,hallDevice[0].W_TADC);
        nowStep=Auto_WriteCalibStop;
        break;
    case Auto_WriteCalibStop:
        hallDevice[0].WriteSingleRegister(0x10,0);
        nowStep=Auto_BurnFinish;
        break;
     case Auto_BurnFinish:
        hallDevice[0].WriteSingleRegister(0x12,0x6a);
        nowStep=Complete;
        break;
     case Complete:
        nowStep=ProcessEnd;
        break;

    }
    if(nowStep!=ProcessEnd)
    {
        ProcessEngine.start(75);
    }else
    {
        ProcessEngine.stop();
        ui->textEdit->append("流程结束");
    }
}

void MainWindow::Slot_StartAutoTest()
{
    isShowedMessageBox=false;
    nowStep=PowerOn;
    for(int i=0;i<=4;i++)
    {
        returnValue[i].isSuccess=false;
        hallDevice[i].isShouldTest=false;
        hallDevice[i].CalibError=ui->lineEdit_CalibError->text().toFloat()/1000;
        hallDevice[i].IsGetZeroLSB=false;
    }
    returnValue[0].isSuccess=true;
    hallDevice[0].isShouldTest=true;

    ProcessEngine.setSingleShot(true);
    ProcessEngine.start(50);
}

ReturnStruct MainWindow::PowerOnChip(int i)
{
    SourcePowerOff();
     ui->textEdit->append("上电v0.002:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
     ui->textEdit->append("开始上电");
     //判断是否需要测试
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    //默认休眠50毫秒
    QThread::msleep(50);
    //上电
    bool success=hallDevice[i].HallChipControl(3);
    if(success)
    {
        returnValue[i].isSuccess=true;
        hallDevice[i].isShouldTest=true;
        returnValue[i].step=StepName::ReadTADC;
    }
    else
    {
        hallDevice[i].isShouldTest=false;
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        ui->textEdit->append(QString("第%0组芯片上电失败").arg(i));
    }
    return  returnValue[i];
}

ReturnStruct MainWindow::ReadTADCFuntion(int i)
{
    ui->textEdit->append("读TADC:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
    ui->textEdit->append("读TADC");
    //读低字节
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    QThread::msleep(50);
    ReadRegisterValue read=hallDevice[i].ReadSingleRegister(07);
    if(read.success)
    {
        hallDevice[i].TADCLow=read.value;
    }
    else
    {
        returnValue[i].isSuccess=false;
        hallDevice[i].isShouldTest=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }

    //读高字节
    QThread::msleep(50);
    read=hallDevice[i].ReadSingleRegister(06);
    if(read.success)
    {
        hallDevice[i].TADCHi=read.value;
    }
    else
    {
        returnValue[i].isSuccess=false;
        hallDevice[i].isShouldTest=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }

    hallDevice[i].TADC=((hallDevice[i].TADCHi<<8)|(hallDevice[i].TADCLow));

    returnValue[i].isSuccess=true;
    hallDevice[i].isShouldTest=true;
    returnValue[i].step=ReadBurnTimes;

    qDebug()<<"[TADC]="<<hallDevice[i].TADC;
    return returnValue[i];
}

ReturnStruct MainWindow::ReadBurnTime(int i)
{
    ui->textEdit->append("读烧写次数:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
     ui->textEdit->append("读烧写次数");
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }

    QThread::msleep(50);
    ReadRegisterValue ret=hallDevice[i].ReadSingleRegister(0x30);


    qDebug()<<"读出来的烧写次数寄存器="<<ret.value;
    if(ret.success)
    {
        returnValue[i].isSuccess=true;
        hallDevice[i].BurnTimes=ret.value;
        if( (hallDevice[i].BurnTimes&0x40) != 0 )               //判断烧录次数MTP_Bank
        {
            ui->textEdit->append("该霍尔已经进行了二次烧录！无法进行烧录");

            returnValue[i].isSuccess=false;
            hallDevice[i].isShouldTest=false;
            returnValue[i].step=ProcessEnd;

            return returnValue[i];
        }
        if(hallDevice[i].BurnTimes== 0)
        {
            //寄存器0x30，决定采用一次还是二次的数据
            hallDevice[i].GS302_R30 = 0xA2;
            hallDevice[i].WReg_GG0  = 0x40;
            hallDevice[i].WReg_VBIAS= 0x41;
            //自动判断存储空间
            hallDevice[i].MTP_Bank  = 1;
            hallDevice[i].LUT_Bank  = 1;
            ui->textEdit->append("该霍尔已经进行了0次烧录！这是第1次烧录");

            returnValue[i].step=StepToWriteMod;
            returnValue[i].isSuccess=true;
            hallDevice[i].isShouldTest=true;
        }
        else
        {
            //寄存器0x30，决定采用一次还是二次的数据
            hallDevice[i].GS302_R30 = 0xE6;
            hallDevice[i].WReg_GG0  = 0x42;
            hallDevice[i].WReg_VBIAS= 0x43;
            //暂时手动决定存储空间
            hallDevice[i].MTP_Bank  = 2;
            hallDevice[i].LUT_Bank  = 3;
            ui->textEdit->append("该霍尔已经进行了1次烧录！这是第2次烧录");

            returnValue[i].step=StepToWriteMod;
            returnValue[i].isSuccess=true;
            hallDevice[i].isShouldTest=true;

        }
    }
    else
    {
        returnValue[i].isSuccess=false;
        hallDevice[i].isShouldTest=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    return returnValue[i];
}

ReturnStruct MainWindow::StepChipToWriteMode(int i)
{
    ui->textEdit->append("进入连续写模式:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
     ui->textEdit->append("连续写模式");
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }

     QThread::msleep(50);
    bool ret=hallDevice[i].HallChipControl(1);
    if(ret)
    {
        returnValue[i].isSuccess=true;
        hallDevice[i].isShouldTest=true;
        returnValue[i].step=WriteTC1;
    }
    else
    {
        returnValue[i].isSuccess=false;
        hallDevice[i].isShouldTest=false;
        returnValue[i].step=ProcessEnd;
    }
    return  returnValue[i];
}

ReturnStruct MainWindow::ChipWriteTC1(int i)
{
    ui->textEdit->append("写TC1:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

     ui->textEdit->append("写TC1");
     QThread::msleep(50);
     hallDevice[i].ChannelSwitch(1);
     QThread::msleep(50);
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }

    if(hallDevice[i].MTP_Bank!=1)
    {
        returnValue[i].isSuccess=true;
        hallDevice[i].isShouldTest=true;
        returnValue[i].step=WriteTC2;
        hallDevice[i].isAlreadyTested=true;
        return returnValue[i];
    }
    else
    {
        hallDevice[i].isAlreadyTested=false;
        hallDevice[i].GS302_TC1=ui->TC1_SpinBox->value();
        hallDevice[i].GS302_TC2=ui->TC2_SpinBox->value();
        bool ret=false;
        ret=hallDevice[i].WriteSingleRegister(0x32,hallDevice[i].GS302_TC1);
        if(ret)
        {
            returnValue[i].isSuccess=true;
            hallDevice[i].isShouldTest=true;
            returnValue[i].step=WriteTC2;
        }
        else
        {
            returnValue[i].isSuccess=false;
            hallDevice[i].isShouldTest=false;
            returnValue[i].step=ProcessEnd;
        }
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipWriteTC2(int i)
{
    ui->textEdit->append("写TC2:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

     ui->textEdit->append("写TC2");
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }

    if(hallDevice[i].isAlreadyTested)
    {
        returnValue[i].step=StartCalibration;
        returnValue[i].isSuccess=true;
        hallDevice[i].isShouldTest=true;
        return returnValue[i];
    }
    QThread::msleep(50);
    hallDevice[i].GS302_TC1=ui->TC1_SpinBox->value();
    hallDevice[i].GS302_TC2=ui->TC2_SpinBox->value();
    bool ret=hallDevice[i].WriteSingleRegister(0x33,hallDevice[i].GS302_TC2);
    if(ret)
    {
        returnValue[i].isSuccess=true;
        hallDevice[i].isShouldTest=true;
        returnValue[i].step=StartCalibration;
    }
    else
    {
        returnValue[i].isSuccess=false;
        hallDevice[i].isShouldTest=false;
        returnValue[i].step=ProcessEnd;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::StartChipCalibration(int i)
{
    ui->textEdit->append("进入调试模式:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

     ui->textEdit->append("进入调试模式");
//    if(!hallDevice[i].isShouldTest)
//    {
//        returnValue[i].isSuccess=false;
//        returnValue[i].step=ProcessEnd;
//        return returnValue[i];
//    }
    QThread::msleep(50);
    bool ret=hallDevice[i].WriteSingleRegister(0x10,1);
    if(ret)
    {
        returnValue[i].step=WriteBurnsNumber;
        returnValue[i].isSuccess=true;
        hallDevice[i].isShouldTest=true;
    }
    else
    {
        returnValue[i].step=ProcessEnd;
        returnValue[i].isSuccess=false;
        hallDevice[i].isShouldTest=false;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipWriteBurnsNumber(int i)
{
    ui->textEdit->append("写烧写次数:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

     ui->textEdit->append("写烧写次数");
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    QThread::msleep(50);
    bool ret=hallDevice[i].WriteSingleRegister(0x30,hallDevice[i].GS302_R30);
    if(ret)
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=WriteVBIAS;
        hallDevice[i].isShouldTest=true;
    }
    else
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipWriteVBIAS(int i)
{
    ui->textEdit->append("写VBIAS:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

     ui->textEdit->append("写VBIAS");
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    hallDevice[i].ChannelSwitch(1);
    GetWriteInBufferValue();
    QThread::msleep(50);
    bool ret=hallDevice[i].WriteSingleRegister(hallDevice[i].WReg_VBIAS,hallDevice[i].GS302_VBIAS);

    if(ret)
    {
        returnValue[i].isSuccess=true;
        hallDevice[i].isShouldTest=true;
        returnValue[i].step=WriteVEXC;
    }
    else
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipWriteVEXC(int i)
{
    ui->textEdit->append("写VEXC:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

     ui->textEdit->append("写VEXC");
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }

    GetWriteInBufferValue();
    QThread::msleep(50);
    bool ret=hallDevice[i].WriteSingleRegister(0x31,hallDevice[i].GS302_R31);
    if(ret)
    {
        returnValue[i].step=WriteEnlarge;
        returnValue[i].isSuccess=true;
        hallDevice[i].isShouldTest=true;
    }
    else
    {
        returnValue[i].step=ProcessEnd;
        returnValue[i].isSuccess=false;
        hallDevice[i].isShouldTest=false;
    }
    return  returnValue[i];
}

ReturnStruct MainWindow::ChipWriteEnlarge(int i)
{
    ui->textEdit->append("写GO:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

     ui->textEdit->append("写G0G1G2");
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    QThread::msleep(50);
    bool ret=hallDevice[i].WriteSingleRegister(hallDevice[i].WReg_GG0,0);
    if(ret)
    {
        hallDevice[i].isShouldTest=true;
        returnValue[i].step=WriteZeroL;
        returnValue[i].isSuccess=true;
    }
    else
    {
        hallDevice[i].isShouldTest=false;
        returnValue[i].step=ProcessEnd;
        returnValue->isSuccess=false;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipWriteZeroL(int i)
{
    ui->textEdit->append("写ZeroL:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

     ui->textEdit->append("写ZEROL");
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    QThread::msleep(50);
    bool ret=hallDevice[i].WriteSingleRegister(0x09,0xff);
    if(ret)
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=WriteZeroH;
        hallDevice[i].isShouldTest=true;
    }
    else
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
    }

    return returnValue[i];
}

ReturnStruct MainWindow::ChipWriteZeroH(int i)
{
    ui->textEdit->append("写ZeroH:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

     ui->textEdit->append("写Zeroh");
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    QThread::msleep(50);
    bool ret=hallDevice[i].WriteSingleRegister(0x08,0x1f);
    if(ret)
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=WriteGainL;
        hallDevice[i].isShouldTest=true;
    }
    else
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
    }
    return returnValue[i];

}

ReturnStruct MainWindow::ChipWriteGainL(int i)
{
    ui->textEdit->append("写GainL:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

     ui->textEdit->append("写Gainl");
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    QThread::msleep(50);
    bool ret=hallDevice[i].WriteSingleRegister(0xB,0);
    if(ret)
    {
        returnValue[i].step=WriteGainH;
        returnValue[i].isSuccess=true;
        hallDevice[i].isShouldTest=true;
    }
    else
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
    }
    return  returnValue[i];
}

ReturnStruct MainWindow::ChipWriteGainH(int i)
{
    ui->textEdit->append("写GainH:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("写GainH");
    //判断是否需要测试
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    //写0x0a
    QThread::msleep(50);
    bool ret=hallDevice[i].WriteSingleRegister(0x0a,0);

    if(ret)
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=SignalPowerOn;
        hallDevice[i].isShouldTest=true;
    }
    else
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }

    QThread::msleep(50);
    //切换置电压测试模式
    ret=hallDevice[i].SwitchToMeasureVoltageMode();
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    QThread::msleep(50);
    //切换置out置adc
    ret=hallDevice[i].ChannelSwitch(0);
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }

    QThread::msleep(600);

    //测试电压
    box[i].ReadVoltageInformation();
    hallDevice[i].Zero_BasicsOutV=box[i].GetRecvVoltageValue();
    if(hallDevice[i].Zero_BasicsOutV!=-1)
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=SignalPowerOn;
        hallDevice[i].isShouldTest=true;
    }
    else
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
    }
    qDebug()<<"[获得采集到的电压]:"<<hallDevice[i].Zero_BasicsOutV;

    ui->textEdit->append(QString("------------------->基础零点=%0").arg(hallDevice[i].Zero_BasicsOutV));
    //切换至out对mcu
    QThread::msleep(1000);
    ret=hallDevice[i].ChannelSwitch(1);
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    else
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=SignalPowerOn;
        hallDevice[i].isShouldTest=true;
    }
    return returnValue[i];

}

ReturnStruct MainWindow::OutSiganlPowerON()
{
    ui->textEdit->append("请上电:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

     ui->textEdit->append("请上电");
    QMessageBox a;
    a.setText("请上电");
    a.setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel);
    a.setDefaultButton(QMessageBox::Ok);
    isShowedMessageBox=true;
    int ret=a.exec();
    switch (ret)
    {
    case QMessageBox::Ok:
        TransmitStep(true);
        qDebug()<<"------------>ok";
        break;
    case QMessageBox::Cancel:
        qDebug()<<"------------>nok";
        TransmitStep(false);
        break;
    }
    return returnValue[0];
}

ReturnStruct MainWindow::SourcePowerOn()
{
    QThread::msleep(1000);
    bool ret=false;
    ret=currentSource.Send50A();
    if(ret)
    {
        TransmitStep(true);
    }
    else
    {
        TransmitStep(false);
    }
    QThread::msleep(2500);
    return returnValue[0];

}

void MainWindow::TransmitStep(bool isOk)
{
    if(isOk)
    {
        for(int i=0;i<=4;i++)
        {
            if(returnValue[i].isSuccess)
            {
                hallDevice[i].isShouldTest=true;
                returnValue[i].step=CaptureVoltage;
            }
        }
    }
    else
    {
        for(int i=0;i<=4;i++)
        {
            returnValue[i].isSuccess=false;
            hallDevice[i].isShouldTest=false;
            returnValue[i].step=ProcessEnd;
        }
    }
}

void MainWindow::TransmitStep(bool bValue, int step)
{
    if(bValue)
    {
        for(int i=0;i<=4;i++)
        {
            if(returnValue[i].isSuccess)
            {
                hallDevice[i].isShouldTest=true;
                returnValue[i].step=step;
            }
        }
    }
    else
    {
        for(int i=0;i<=4;i++)
        {
            returnValue[i].isSuccess=false;
            hallDevice[i].isShouldTest=false;
            returnValue[i].step=ProcessEnd;
        }
    }
}

ReturnStruct MainWindow::ChipCaptureVoltage(int i)
{
    //ui->textEdit->append(QString("上电获取电压%0").arg(hallDevice[i].Full_BasicsOutV));
    ui->textEdit->append("上电获取电压:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    //判断是否需要测试
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    QThread::msleep(50);
    //------------------切换置电压模式--------------------//
    bool ret=hallDevice[i].SwitchToMeasureVoltageMode();
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    QThread::msleep(50);
    //------------------切换置out置adc-------------------
    ret=hallDevice[i].ChannelSwitch(0);
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }

    QThread::msleep(600);

    //----------------------测试电压
    box[i].ReadVoltageInformation();
    hallDevice[i].Full_BasicsOutV=box[i].GetRecvVoltageValue();
    if(hallDevice[i].Full_BasicsOutV!=-1)
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=GetGain;
        hallDevice[i].isShouldTest=true;
         ui->textEdit->append(QString("-------->基础满值=%0").arg(hallDevice[i].Full_BasicsOutV));
    }
    else
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    qDebug()<<"[获得采集到的电压]:"<<hallDevice[i].Full_BasicsOutV;

    QThread::msleep(1000);
    //切换至out对mcu
    ret=hallDevice[i].ChannelSwitch(1);
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    else
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=GetGain;
        hallDevice[i].isShouldTest=true;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipGetGain(int i)
{
    ui->textEdit->append("上电获取电压调试获取gain:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("上电获取电压调试获取gain");

    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }


    //获取 内部零点  外部零点  期望满值
    hallDevice[i].Zero_ExpectInternalRef  = ui->lineEdit_InternalRefZero->text().toFloat();       //获取内部参考期望零点--内部参考
    hallDevice[i].Zero_ExpectActualRef = ui->lineEdit_ActualRefZero->text().toFloat()/1000.0;     //获取实际外部期望零点--外部参考
    hallDevice[i].Full_ExpectVBIAS  = ui->lineEdit_ExpectUn->text().toFloat();                    //获取期望满值

    hallDevice[i].Un_ExpectVBIAS = abs(hallDevice[i].Full_ExpectVBIAS - hallDevice[i].Zero_ExpectActualRef);                 //实际的量程期望值
    hallDevice[i].RealTimeRef_Zero = hallDevice[i].Zero_ExpectInternalRef;                                      //获取实时参考零点
    hallDevice[i].RealTimeRef_Un   = hallDevice[i].Un_ExpectVBIAS;                                              //获取实时参考量程

    ui->textEdit->append(QString("---------->期望量程%0").arg(hallDevice[i].RealTimeRef_Un ));

    GetGain_enumG3(hallDevice[i].Un_ExpectVBIAS,i);

    ui->comboBox_G0->setCurrentText(QString("%1").arg(hallDevice[i].Go_Gain));
    ui->comboBox_G1->setCurrentText(QString("%1").arg(hallDevice[i].G1_Gain));
    ui->comboBox_G2->setCurrentText(QString("%1").arg(hallDevice[i].G2_Gain));
    ui->label_FloatGain->setText(QString("%1").arg(hallDevice[i].GainDAC));
    ui->label_FloatZero->setText(QString("%1").arg(hallDevice[i].ZeroDAC));

    hallDevice[i].GS302_GainDAC =  hallDevice[i].GainDAC*1.5-0.5;     //灵敏细调同步到输出
    hallDevice[i].GS302_ZeroDAC =hallDevice[i].ZeroDAC;
    if(hallDevice[i].ZeroDAC==0||hallDevice[i].GainDAC==0)
    {
        //        hallDevice[i].isShouldTest=false;
        //        returnValue[i].isSuccess=false;
        //        returnValue[i].step=ProcessEnd;
    }
    hallDevice[i].isShouldTest=true;
    returnValue[i].isSuccess=true;
    returnValue[i].step=Auto_RoughToWrite;
}

void MainWindow::GetGain_enumG3(float ActualRange,int i)
{
    float GetGain,GetZero,Multiple,Diffrence,Product,ClosestProduct,Zero_PianZhi;//获取增益、获取零点细调、整体放大倍数、差值、乘积、向量积、零点偏置电压、实际量程
    uint8_t Cnt1,Cnt2,Cnt3;
    float ClosestG1,ClosestG2,ClosestGo;            //1~3阶实际增益
    float G1[] = {1,2,4,8,16,32,64,128};            //一阶增益
    float G2[] = {1.182,1.4,1.667,2,2.429,3,3.5,4}; //二阶增益
    float Go[] = {2.4,2.8,3.2,3.6};                 //三阶增益

    /* 计算霍尔电压，即输入信号电压 灵敏为0时、零点为0.5时 0.8 = Go[0]*0.3333 */
    Zero_PianZhi =hallDevice[i].GS302_VEXC*8191/16383;
    hallDevice[i].Zero_InternalInV = ((hallDevice[i].Zero_BasicsOutV/0.8)-Zero_PianZhi)/1.182;
    hallDevice[i].Full_InternalInV = ((hallDevice[i].Full_BasicsOutV/0.8)-Zero_PianZhi)/1.182;
    /* 反推所有粗调增益系数 */
    /* 反推所有粗调增益系数 */
    if(true)//去除零点失调
    {
        hallDevice[i].Un_InternalInV = hallDevice[i].Full_InternalInV - hallDevice[i].Zero_InternalInV;               //霍尔电压量程差值
    }
    else
    {
        hallDevice[i].Un_InternalInV = hallDevice[i].Full_InternalInV - 0;                      //霍尔电压量程差值，保留零点失调
        ui->textEdit->append(QString("basic out  V=%0").arg(hallDevice[i].Full_BasicsOutV));
        ui->textEdit->append(QString("Internal V=%0").arg(hallDevice[i].Full_InternalInV));
    }
    if(hallDevice[i].Un_InternalInV > 0)
    {
        Multiple = ActualRange/hallDevice[i].Un_InternalInV/0.6;
    }         //求内部增益倍数,以接近一半的增益系数作为参考
    else
    {
        hallDevice[i].Un_InternalInV = 0;
        //CreateMessage("请确认输入信号方向是否正确！");
        return;
    }

    /* 开始遍历枚举增益倍率 */
    ClosestProduct = 9999;                          //用于判断的放大倍数，给一个极大值
    Product = 0;
    ClosestG1 =1,ClosestG2 = 1.182,ClosestGo = 2.4;
    for(Cnt1=0; Cnt1<8;Cnt1++)
    {
        for(Cnt2=0; Cnt2<8;Cnt2++)
        {
            for(Cnt3=0; Cnt3<4;Cnt3++)
            {
                Product = G1[Cnt1]*G2[Cnt2]*Go[Cnt3];
                Diffrence = abs(Product - Multiple);
                if(Diffrence < abs(ClosestProduct - Multiple))
                {
                    ClosestProduct = Product;       //最终积
                    ClosestG1 = G1[Cnt1];           //一阶增益倍率
                    ClosestG2 = G2[Cnt2];           //二阶增益倍率
                    ClosestGo = Go[Cnt3];           //三阶增益倍率
                }
            }
        }
    }
    /* 开始反推细调增益系数*/
    GetGain = Multiple*0.6/ClosestProduct;
    GetZero = (hallDevice[i].Zero_ExpectInternalRef/GetGain/ClosestGo)/hallDevice[i].GS302_VEXC;
    /* 接收灵敏细调 */
    if((GetGain >= (1/3)) && (GetGain <= 1))
    { hallDevice[i].GainDAC = GetGain; }
    else
    { hallDevice[i].GainDAC = 0; }
    //接收零点细调
    if((GetZero>=0) && (GetZero <= 1))
    {
        hallDevice[i].ZeroDAC = GetZero;
    }
    else
    { hallDevice[i].ZeroDAC = 0; }

    hallDevice[i].G1_Gain = ClosestG1;                            //反馈一阶增益
    hallDevice[i].G2_Gain = ClosestG2;                            //反馈二阶增益
    hallDevice[i].Go_Gain = ClosestGo;                            //反馈三阶增益
    ui->textEdit->append(QString("g1=%0").arg(hallDevice[i].G1_Gain));
    ui->textEdit->append(QString("g2=%0").arg(hallDevice[i].G2_Gain));
    ui->textEdit->append(QString("go=%0").arg(hallDevice[i].Go_Gain));
    ui->textEdit->append(QString("gaindac=%0").arg(hallDevice[i].GainDAC ));
    ui->textEdit->append(QString("zerodac=%0").arg(hallDevice[i].ZeroDAC));
}

ReturnStruct MainWindow::ChipRoughToWrite(int i)
{
    ui->textEdit->append("写gain1Gain2Gain3:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
    ui->textEdit->append("写Gain1 gain2 gain3");

//    if(!hallDevice[i].isShouldTest)
//    {
//        returnValue[i].isSuccess=false;
//        returnValue[i].step=ProcessEnd;
//        return returnValue[i];
//    }

    hallDevice[i].ChannelSwitch(1);
    QThread::msleep(500);
    GetWriteInBufferValue();
    //StartChipCalibration(i);
    QThread::msleep(120);
    bool ret=hallDevice[i].WriteSingleRegister(hallDevice[i].WReg_GG0,hallDevice[i].GS302_GG0);
    if(ret)
    {
        hallDevice[i].isShouldTest=true;
        returnValue[i].isSuccess=true;
        returnValue[i].step=WriteRoughZeroL;
    }
    else
    {
        hallDevice[i].isShouldTest=false;
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipRoughWriteZeroL(int i)
{
    ui->textEdit->append("写调试Zerol:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("写调试Zerol");
    QThread::msleep(120);
//    if(!hallDevice[i].isShouldTest)
//    {
//        returnValue[i].isSuccess=false;
//        returnValue[i].step=ProcessEnd;
//        return returnValue[i];
//    }

    bool ret=hallDevice[i].WriteSingleRegister(0x09,hallDevice[i].GS302_K0L);
    if(ret)
    {
        hallDevice[i].isShouldTest=true;
        returnValue[i].isSuccess=true;
        returnValue[i].step=WriteRoughZeroH;
    }
    else
    {
        hallDevice[i].isShouldTest=false;
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipRoughWriteZeroH(int i)
{
    ui->textEdit->append("写调试ZeroH:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("写调试ZeroH");
    QThread::msleep(120);

//    if(!hallDevice[i].isShouldTest)
//    {
//        returnValue[i].isSuccess=false;
//        returnValue[i].step=ProcessEnd;
//        return returnValue[i];
//    }

    bool ret=hallDevice[i].WriteSingleRegister(0x08,hallDevice[i].GS302_K0H);
    if(ret)
    {
        hallDevice[i].isShouldTest=true;
        returnValue[i].isSuccess=true;
        returnValue[i].step=WriteRoughGainL;
    }
    else
    {
        hallDevice[i].isShouldTest=false;
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipRoughWriteGainL(int i)
{
    ui->textEdit->append("写Gainl:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("写gainL");
    QThread::msleep(120);

//    if(!hallDevice[i].isShouldTest)
//    {
//        returnValue[i].isSuccess=false;
//        returnValue[i].step=ProcessEnd;
//        return returnValue[i];
//    }

    bool ret=hallDevice[i].WriteSingleRegister(0x0B,hallDevice[i].GS302_KxL);
    if(ret)
    {
        hallDevice[i].isShouldTest=true;
        returnValue[i].isSuccess=true;
        returnValue[i].step=WriteRoughGainH;
    }
    else
    {
        hallDevice[i].isShouldTest=false;
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipRoughWriteGainH(int i)
{
    ui->textEdit->append("写调试GainH:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("写gainH");
    QThread::msleep(120);

//    if(!hallDevice[i].isShouldTest)
//    {
//        returnValue[i].isSuccess=false;
//        returnValue[i].step=ProcessEnd;
//        return returnValue[i];
//    }

    bool ret=hallDevice[i].WriteSingleRegister(0x0A,hallDevice[i].GS302_KxH);
    if(ret)
    {
        hallDevice[i].isShouldTest=true;
        returnValue[i].isSuccess=true;
        returnValue[i].step=CaptureAfterRoughVoltage;
    }
    else
    {
        hallDevice[i].isShouldTest=false;
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipCaptureAfterRoughVoltage(int i)
{
    ui->textEdit->append("获取电压:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("获取写入后的电压");

    //判断是否需要测试
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    //------------------切换置电压模式--------------------//
    bool ret=hallDevice[i].SwitchToMeasureVoltageMode();
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    QThread::msleep(50);
    //------------------切换置out置adc-------------------
    ret=hallDevice[i].ChannelSwitch(0);
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }

    QThread::msleep(600);

    //----------------------测试电压
    box[i].ReadVoltageInformation();
    hallDevice[i].Calib_FullOutV=box[i].GetRecvVoltageValue();
    if(hallDevice[i].Calib_FullOutV!=-1)
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=SignalPowerOff;
        hallDevice[i].isShouldTest=true;
    }
    else
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
    }
    ui->textEdit->append(QString("--------->调试满值=%0").arg(hallDevice[i].Calib_FullOutV));
    qDebug()<<"[获得采集到的电压]:"<<hallDevice[i].Calib_FullOutV;
    QThread::msleep(1000);

    //切换至out对mcu
    ret=hallDevice[i].ChannelSwitch(1);
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    else
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=SignalPowerOff;
        hallDevice[i].isShouldTest=true;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::OutSiganlPowerOff()
{
    ui->textEdit->append("请下电:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    QMessageBox a;
    a.setText("请下电");
    a.setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel);
    a.setDefaultButton(QMessageBox::Ok);
    isShowedMessageBox=true;
    int ret=a.exec();
    switch (ret)
    {
    case QMessageBox::Ok:
        TransmitStep(true,CaptureAfterPoweroffVoltage);
        break;
    case QMessageBox::Cancel:
        TransmitStep(false);
        break;
    }
    return returnValue[0];
}

ReturnStruct MainWindow::SourcePowerOff()
{
    QThread::msleep(1000);
    bool ret=false;
    ret=currentSource.Send0A();
    if(ret==true)
    {
        TransmitStep(true,CaptureAfterPoweroffVoltage);
    }
    else
    {
        TransmitStep(false);
    }
    QThread::msleep(2500);
    return returnValue[0];

}

ReturnStruct MainWindow::ChipCaptureAfterPoweroffVoltage(int i)
{
    ui->textEdit->append("获得下电后的电压:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("获取下电后的电压");

    //判断是否需要测试
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    //------------------切换置电压模式--------------------//
    bool ret=hallDevice[i].SwitchToMeasureVoltageMode();
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    QThread::msleep(50);
    //------------------切换置out置adc-------------------
    ret=hallDevice[i].ChannelSwitch(0);
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }

    QThread::msleep(600);

    //----------------------测试电压
    box[i].ReadVoltageInformation();

    float TempData=0;
    TempData=box[i].GetRecvVoltageValue();
    if( hallDevice[i].IsGetZeroLSB)
    {
        hallDevice[i].IsGetZeroLSB = false;
        hallDevice[i].ZeroVChange = hallDevice[i].Calib_ZeroOutV - TempData;
        hallDevice[i].ZeroLSB_Value = abs(hallDevice[i].ZeroVChange/hallDevice[i].ZeroWriteChange);
    }

    ui->textEdit->append(QString("--------->调试零点%0").arg(TempData));


    hallDevice[i].Calib_ZeroOutV=TempData;
    if(hallDevice[i].Calib_ZeroOutV!=-1)
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=Auto_FineGetUnError;
        hallDevice[i].isShouldTest=true;
    }
    else
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    qDebug()<<"[获得采集到的电压]:"<<hallDevice[i].Calib_ZeroOutV;

    QThread::msleep(1000);
    //切换至out对mcu
    ret=hallDevice[i].ChannelSwitch(1);
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    else
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=Auto_FineGetUnError;
        hallDevice[i].isShouldTest=true;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipGetUnError(int i)
{
    ui->textEdit->append("获得量程误差:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("获取量程误差");
    //判断是否需要测试
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }

    hallDevice[i].Calib_Un = hallDevice[i].Calib_FullOutV - hallDevice[i].Calib_ZeroOutV;
    hallDevice[i].Calib_UnError = hallDevice[i].Un_ExpectVBIAS - hallDevice[i].Calib_Un;

    hallDevice[i].isShouldTest=true;
    returnValue[i].isSuccess=true;
    returnValue[i].step=Auto_FineGetGain;

    return returnValue[i];
}

ReturnStruct MainWindow::ChipFineGetGain(int i)
{
    ui->textEdit->append("获得Gain:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append(QString("[下电后获取到的量程误差为]=%0").arg(hallDevice[i].Calib_UnError));
    bool ret=setGainZero(hallDevice[i].Calib_UnError,2,i);
    if(ret)
    {
        ui->textEdit->append("ret=true");
        hallDevice[i].isShouldTest=true;
        returnValue[i].isSuccess=true;
        returnValue[i].step=Auto_FineGainL;
        toStep=Auto_FineGetZeroError;
    }
    else
    {
        ui->textEdit->append("ret=false");
        hallDevice[i].isShouldTest=false;
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
    }
    return returnValue[i];
}
QJsonObject MainWindow::AddJsonObj(QString ObjName)
{
    QJsonObject TempObj;    //当前霍尔对象

    TempObj["name"]         = ObjName;
    TempObj["Go"]           = QString("%1").arg(ui->comboBox_G0->currentIndex());
    TempObj["G1"]           = QString("%1").arg(ui->comboBox_G1->currentIndex());
    TempObj["G2"]           = QString("%1").arg(ui->comboBox_G2->currentIndex());

    TempObj["EXC"]          = QString("%1").arg(ui->comboBox_EXC->currentIndex());
    TempObj["SWAP"]         = QString("%1").arg(ui->comboBox_SWAP->currentIndex());
    TempObj["VEXC"]         = QString("%1").arg(ui->comboBox_VEXC->currentIndex());
    TempObj["VBIAS"]        = QString("%1").arg(ui->comboBox_VBIAS->currentIndex());
    TempObj["Bandwidth"]    = QString("%1").arg(ui->comboBox_Bandwidth->currentIndex());
    TempObj["TC1"]          = QString("%1").arg(ui->TC1_SpinBox->value());
    TempObj["TC2"]          = QString("%1").arg(ui->TC2_SpinBox->value());
    TempObj["ExpectUn"]     = QString("%1").arg(ui->lineEdit_ExpectUn->text());
    TempObj["ExpectZero"]   = QString("%1").arg(ui->lineEdit_InternalRefZero->text());
    TempObj["ChaFenZero"]   = QString("%1").arg(ui->lineEdit_ActualRefZero->text());
    //    TempObj["ChaFen_Mod"]   = QString("%1").arg(ui->checkBox_ChaFen->isChecked());
    TempObj["Zero"]         = ui->label_FloatZero->text();
    TempObj["Gain"]         = ui->label_FloatGain->text();
    TempObj["CalibError"]   = ui->lineEdit_CalibError->text();

    return TempObj;
}

void MainWindow::AddProductModel()
{
    bool ok;
    int  ArrayLength;
    QJsonObject TempObj;                    //当前霍尔对象

    QString text = QInputDialog::getText(nullptr, "添加霍尔型号", "请输入霍尔型号:", QLineEdit::Normal, "", &ok);
    if ( (ok && !text.isEmpty()) && ( !WriteFlash.contains(text)) )
    {
        qDebug() << "输入的文本为：" << text;

        TempObj = AddJsonObj(text);         //添加带霍尔型号参数的Json对象
        HallArray.append(TempObj);
        ArrayLength = HallArray.size();
        HallIndex   = ArrayLength-1;

        WriteFlash.append(text);
        ui->comboBox_11->clear();
        ui->comboBox_11->addItems(WriteFlash);
        ui->comboBox_11->setCurrentIndex(HallIndex);

        QMessageBox::information(this,"提示","霍尔型号 添加成功！请确认配置参数并保存！");
    }
    else
    {
        QMessageBox::warning(this,"警告","霍尔型号 添加失败！请检查是否重复添加,或者输入为空！");
    }
}

QString MainWindow::CreateJsonObj(QString ObjName)
{
    QJsonObject HallObj,TempObj;    //当前霍尔对象
    QJsonArray TempArray;           //霍尔所有类型数组
    QJsonDocument HallSaveDoc;      //保存霍尔类型文档

    TempObj = AddJsonObj(ObjName);  //添加带霍尔型号参数的Json对象
    //添加到对象数组中
    TempArray.append(TempObj);



    HallIndex =  ui->comboBox_11->currentIndex();
    HallArray[HallIndex] =  TempArray[0];

    //将所有霍尔类型添加到霍尔对象
    HallObj["霍尔数量"]      = HallArray.size();
    HallObj["霍尔类型"]      = HallArray;
    //转换为文档对象
    HallSaveDoc              =  QJsonDocument::fromVariant(HallObj.toVariantMap());

    return HallSaveDoc.toJson(QJsonDocument::Indented);
}


void MainWindow::WriteJson(QString HallName)
{

    QFile file("./霍尔配置参数.json");
    QTextStream stream(&file);
    if (file.open(QIODevice::ReadWrite | QIODevice::Text | QFile::Truncate))
    {
        QTextStream stream(&file);
        stream << CreateJsonObj(HallName);
        file.close();
        QMessageBox::information(this,"提示信息","数据保存成功!");
    }
}

void MainWindow::SaveHallConfig()
{
    WriteJson(ui->comboBox_11->currentText());
    qDebug()<<__FUNCTION__;
}

void MainWindow::LoadHallConfig()
{
    qDebug()<<__FUNCTION__;
    PharseConfigFile();
}

void MainWindow::Slot_ShowString(QString recv)
{
    ui->textEdit->append(recv);
}

void MainWindow::BurnAll()
{
    qDebug()<<__FUNCTION__;
    LoadCalibrationData(0);
    nowStep=Auto_BurnStart;
    ProcessEngine.setSingleShot(true);
    ProcessEngine.start(50);
}

void MainWindow::LoadCalibrationData(int i)
{
    uint16_t TempZero,TempGain,TempTADC;

    TempZero = hallDevice[i].GS302_ZeroDAC*16383;
    TempGain = hallDevice[i].GS302_GainDAC*16383;
    TempTADC = ( (((uint16_t)hallDevice[i].TADCHi)<<8) & 0xFF00) + (((uint16_t)hallDevice[i].TADCLow)&0xFF);
    //根据烧写区域来划分需要写入的寄存器地址
    if(hallDevice[i].LUT_Bank == 7)
    {
        hallDevice[i].WReg_GainDACL = 0x82;
        hallDevice[i].WReg_GainDACH = 0x81;
        hallDevice[i].WReg_ZeroDACL = 0x80;
        hallDevice[i].WReg_ZeroDACH = 0x7F;
        hallDevice[i].WReg_TADC     = 0x7E;
    }
    else if( hallDevice[i].LUT_Bank == 3 )
    {
        hallDevice[i].WReg_GainDACL = 0x6E;
        hallDevice[i].WReg_GainDACH = 0x6D;
        hallDevice[i].WReg_ZeroDACL = 0x6C;
        hallDevice[i].WReg_ZeroDACH = 0x6B;
        hallDevice[i].WReg_TADC     = 0x6A;
    }
    else
    {
        hallDevice[i].WReg_GainDACL = 0x64;
        hallDevice[i].WReg_GainDACH = 0x63;
        hallDevice[i].WReg_ZeroDACL = 0x62;
        hallDevice[i].WReg_ZeroDACH = 0x61;
        hallDevice[i].WReg_TADC     = 0x60;
    }

    hallDevice[i].W_GainDACL = TempGain&0xFF;
    hallDevice[i].W_GainDACH = ((TempGain>>8)&0x3F) + ((TempZero<<6)&0xC0); //保留零点<1:0>共计2位-高,灵敏度<13:8>共计6位-低
    hallDevice[i].W_ZeroDACL = (TempZero>>2)&0xFF;                          //保留零点<9:2>共计8位
    hallDevice[i].W_ZeroDACH = ((TempTADC<<4)&0xF0) + ((TempZero>>10)&0x0F);//保留零点<13:8>共计4位-高,零点<13:10>共计4位-低
    hallDevice[i].W_TADC     = (TempTADC>>4)&0xFF;                          //保留温度传感器<11:4>共计8位
}


bool MainWindow::setGainZero(float DifferenceValue,int mode,int i)
{
    uint16_t TempData;

    float TempVOut,Multiple,ClosestProduct;                         //排除3阶增益、内部输入信号、总增益倍数、交叉乘积


    TempVOut = (hallDevice[i].RealTimeRef_Zero + DifferenceValue)/hallDevice[i].Go_Gain;

    if(mode == 0)                                                   //Zero零点增益
    {
        if( hallDevice[i].GainDAC != 0 )
        {
            hallDevice[i].ZeroDAC = TempVOut/hallDevice[i].GainDAC/hallDevice[i].GS302_VEXC;
            if( (hallDevice[i].ZeroDAC < 0) ||  (hallDevice[i].ZeroDAC > 1) )
            {
                hallDevice[i].ZeroDAC =hallDevice[i].GS302_ZeroDAC;                 //计算结果有误，则复位历史数据
                //CreateMessage("零点细调计算异常！");
                return false;
            }
            hallDevice[i].RealTimeRef_Zero += DifferenceValue;
        }
        else
        {
            // CreateMessage("灵敏度异常！");
            return false;
        }
        //输出计算结果
        hallDevice[i].GS302_ZeroDAC = hallDevice[i].ZeroDAC;
        TempData = hallDevice[i].GS302_ZeroDAC*16383;
        //二次采集零点输入值并获得零点输入值变化量
        hallDevice[i].ZeroWrite_After = TempData;
        hallDevice[i].IsGetZeroLSB = false;
        hallDevice[i].ZeroWriteChange = abs(hallDevice[i].ZeroWrite_After - hallDevice[i].ZeroWrite_Before);

        hallDevice[i].GS302_K0H = TempData>>8;
        hallDevice[i].GS302_K0L = TempData&0xFF;
        ui->label_FloatZero->setText(QString("%1").arg(hallDevice[i].ZeroDAC));

    }
    else                                                            //灵敏度的新算法
    {
        ClosestProduct = hallDevice[i].G1_Gain*hallDevice[i].G2_Gain*hallDevice[i].Go_Gain;                   //三阶增益总倍率
        if(hallDevice[i].Un_InternalInV > 0)
        {
            hallDevice[i].RealTimeRef_Un += DifferenceValue;
            Multiple = hallDevice[i].RealTimeRef_Un/hallDevice[i].Un_InternalInV;               //通过粗调霍尔灵敏度反推放大倍数
        }
        else
        {
            ui->textEdit->append(QString("Internal V=%0").arg(hallDevice[i].Full_InternalInV));
            ui->textEdit->append(QString("输入信号异常=%0").arg(hallDevice[i].Un_InternalInV));
            hallDevice[i].Un_InternalInV = 0;
            return false;
        }
        hallDevice[i].GainDAC = Multiple/ClosestProduct;
        if((hallDevice[i].GainDAC<((float)1/3)) || (hallDevice[i].GainDAC>1))
        {
            ui->textEdit->append(QString("灵敏度计算结果异常=%0").arg(hallDevice[i].GainDAC));
            hallDevice[i].GainDAC = (hallDevice[i].GS302_GainDAC+0.5)/1.5;           //计算结果异常,复位历史增益数据
            hallDevice[i].RealTimeRef_Un -= DifferenceValue;
            //CreateMessage("灵敏度计算结果异常！");
            ui->textEdit->append(QString("灵敏度计算结果异常=%0").arg(hallDevice[i].GainDAC));

            return false;
        }
        //输出计算结果
        hallDevice[i].GS302_GainDAC = hallDevice[i].GainDAC*1.5-0.5;
        TempData = hallDevice[i].GS302_GainDAC*16383;
        hallDevice[i].GS302_KxH = TempData>>8;
        hallDevice[i].GS302_KxL = TempData&0xFF;
        ui->label_FloatGain->setText(QString("%1").arg(hallDevice[i].GainDAC));

        hallDevice[i].ZeroWrite_Before = hallDevice[i].ZeroDAC*16383;                //接收灵敏校准的零点，之后采集的为第一次零点
    }

    return  true;
}

ReturnStruct MainWindow::ChipFineWriteGainL(int i)
{
    ui->textEdit->append("写GainL:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    //判断是否需要测试
    hallDevice[0].ChannelSwitch(1);
    QThread::msleep(50);
    ui->textEdit->append("写GainL");
    QThread::msleep(120);
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }

    bool ret=hallDevice[i].WriteSingleRegister(0x0B,hallDevice[i].GS302_KxL);
    if(ret)
    {
        returnValue[i].isSuccess=true;
        hallDevice[i].isShouldTest=true;
        returnValue[i].step=Auto_FineGainH;
    }
    else
    {
        returnValue[i].isSuccess=false;
        hallDevice[i].isShouldTest=false;
        returnValue[i].step=ProcessEnd;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::ChipFineWriteGainH(int i)
{
    ui->textEdit->append("写GainH:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("写gainH");
    QThread::msleep(120);
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }

    bool ret=hallDevice[i].WriteSingleRegister(0x0A,hallDevice[i].GS302_KxH);
    if(ret)
    {
        returnValue[i].isSuccess=true;
        hallDevice[i].isShouldTest=true;
        returnValue[i].step=Auto_LookOutStart;
    }
    else
    {
        returnValue[i].isSuccess=false;
        hallDevice[i].isShouldTest=false;
        returnValue[i].step=ProcessEnd;
    }
    return returnValue[i];
}

ReturnStruct MainWindow::NormalGetVoltage(int i)
{
    ui->textEdit->append("普通获得电压:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("普通获取电压");

    //判断是否需要测试
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    //------------------切换置电压模式--------------------//
    bool ret=hallDevice[i].SwitchToMeasureVoltageMode();
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    QThread::msleep(50);
    //------------------切换置out置adc-------------------
    ret=hallDevice[i].ChannelSwitch(0);
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }

    QThread::msleep(600);

    //----------------------测试电压
    box[i].ReadVoltageInformation();

    Get_Voltage=box[i].GetRecvVoltageValue();
    ui->textEdit->append(QString("[获取到的普通电压]=%0").arg(Get_Voltage));
    if(Get_Voltage!=-1)
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=toStep;
        hallDevice[i].isShouldTest=true;
    }
    else
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    qDebug()<<"[获得采集到的电压]:"<<hallDevice[i].Calib_ZeroOutV;

    QThread::msleep(1000);
    //切换至out对mcu
    ret=hallDevice[i].ChannelSwitch(1);
    if(!ret)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        hallDevice[i].isShouldTest=false;
        return returnValue[i];
    }
    else
    {
        returnValue[i].isSuccess=true;
        returnValue[i].step=toStep;
        hallDevice[i].isShouldTest=true;
    }

    return returnValue[i];
}

ReturnStruct MainWindow::ChipFineGetZeroError(int i)
{
    ui->textEdit->append("获取零点误差:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("获取零点误差");

    hallDevice[i].IsGetZeroLSB = false;                        //在此确保截取的变量动作未开始
    hallDevice[i].Aotu_LoadParam(Get_Voltage,"调试零点");       //采样值录入，-------------------->零点一次采样
    //计算当前电压误差值
    hallDevice[i].Calib_ZeroError = hallDevice[i].Zero_ExpectActualRef - hallDevice[i].Calib_ZeroOutV;

    hallDevice[i].isShouldTest=true;
    returnValue[i].isSuccess=true;
    returnValue[i].step=Auto_FineGetZero;
    return returnValue[i];
}

ReturnStruct MainWindow::ChipFineGetZero(int i)
{
    ui->textEdit->append("计算ZeroDac:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("计算zeroDac");

    if(abs(hallDevice[i].Calib_ZeroError)>hallDevice[i].CalibError)         //误差超过1mV时重新计算零点
    {
        returnValue[i].step =  Auto_FineZeroL;                         //先写低字节，下一步写高字节
        returnValue[i].isSuccess=true;
        hallDevice[i].isShouldTest=true;
        toStep = Auto_FineAloneZero1;
        setGainZero(hallDevice[i].Calib_ZeroError,0,i);   //零点粗调增益计算，拿到二次采样零点---写入值

        // HintTextSetColor("细调■■■","color: black");
        //ComContinueTimer->start(INSTRUCTION_DELAY/2);   //100ms后开始写入零点值.
        //SetProgressBar("自动校准",true,88);
    }
    else
    {
        returnValue[i].step =  Complete;
        returnValue[i].isSuccess=true;
        ui->textEdit->append("校准完成");
        //HintTextSetColor("校准完成！","color: green");
        //SetProgressBar("自动校准",true,100);
    }
}

ReturnStruct MainWindow::ChipFineWriteZeroL(int i)
{
    ui->textEdit->append("写调试ZeroL:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    //判断是否需要测试
    ui->textEdit->append("写ZeroL");

    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
    QThread::msleep(120);
    hallDevice[0].ChannelSwitch(1);
    QThread::msleep(50);
    bool ret=hallDevice[i].WriteSingleRegister(0x09,hallDevice[i].GS302_K0L);
    if(ret)
    {
        hallDevice[i].isShouldTest=true;
        returnValue[i].isSuccess=true;
        returnValue[i].step=Auto_FineZeroH;
        return returnValue[i];
    }
    else
    {
        hallDevice[i].isShouldTest=false;
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
}

ReturnStruct MainWindow::ChipFineWriteZeroH(int i)
{
    ui->textEdit->append("写调试Zeroh:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("写ZeroH");
    QThread::msleep(50);
    //判断是否需要测试
    if(!hallDevice[i].isShouldTest)
    {
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }

    bool ret=hallDevice[i].WriteSingleRegister(0x08,hallDevice[i].GS302_K0H);
    if(ret)
    {
        hallDevice[i].isShouldTest=true;
        returnValue[i].isSuccess=true;
        returnValue[i].step=Auto_LookOutStart;
        return returnValue[i];
    }
    else
    {
        hallDevice[i].isShouldTest=false;
        returnValue[i].isSuccess=false;
        returnValue[i].step=ProcessEnd;
        return returnValue[i];
    }
}

ReturnStruct MainWindow::ChipFineAloneZero1(int i)
{
    ui->textEdit->append("计算零点误差:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("新计算量程误差");

    hallDevice[i].IsGetZeroLSB = true;                         //计算零点低位电压值，一般在二次采样零点时只计算一次
    hallDevice[i].Aotu_LoadParam(Get_Voltage,"调试零点");       //采样值录入，-------------------零点二次采样
    //零点误差计算
    hallDevice[i].Calib_ZeroError = hallDevice[i].Zero_ExpectActualRef  - hallDevice[i].Calib_ZeroOutV;
  //  ComContinueTimer->start(INSTRUCTION_DELAY);         //延时0.2S触发下一次指令执行
   // SetProgressBar("自动校准",true,93);
    returnValue[i].step=Auto_FineAloneZero3;
    returnValue[i].isSuccess=true;
    hallDevice[i].isShouldTest=true;
    return returnValue[i];
}

ReturnStruct MainWindow::ChipFineAloneZero3(int i)
{
    ui->textEdit->append("获取ZeroDAC:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("新获取Zerodac3");

    if(abs(hallDevice[i].Calib_ZeroError)>hallDevice[i].CalibError)         //误差超过1mV时重新计算零点
    {
        hallDevice[i].ZeroCalibration(hallDevice[i].Calib_ZeroError); //零点细调增益计算
        returnValue[i].step =  Auto_FineZeroL;
        toStep = Auto_FineAloneZero2;
       // HintTextSetColor("细调■■","color: black");
       // ComContinueTimer->start(INSTRUCTION_DELAY/2);   //100ms后开始写入零点值
    }
    else
    {
        qDebug()<<"success";
        returnValue[i].step =  Complete;
        ui->textEdit->append("------->校准完成");
       // HintTextSetColor("校准完成！","color: green");
      //  SetProgressBar("自动校准",true,100);
    }
}

ReturnStruct MainWindow::ChipFineAloneZero2(int i)
{
    ui->textEdit->append("获取Zero误差2:"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));

    ui->textEdit->append("获取Zero误差2");

    hallDevice[i].IsGetZeroLSB = false;
    hallDevice[i].Aotu_LoadParam(Get_Voltage,"调试零点");       //采样值录入，零点二次采样
    //当前零点误差电压计算
    hallDevice[i].Calib_ZeroError = hallDevice[i].Zero_ExpectActualRef  - hallDevice[i].Calib_ZeroOutV;

    returnValue[i].step=Auto_FineAloneZero3;
    returnValue[i].isSuccess=true;
    hallDevice[i].isShouldTest=true;

    ui->textEdit->append("获取Zero误差2返回");
    return returnValue[i];
    //HintTextSetColor("细调■■■","color: black");
    //ComContinueTimer->start(INSTRUCTION_DELAY);         //延时0.2S触发下一次指令执行
}

void MainWindow::HandleWrite()
{
ui->textEdit->append("+0.1手动书写");
hallDevice[0].GS302_GainDAC = ui->label_FloatGain->text().toFloat()*1.5-0.5;
hallDevice[0].GS302_ZeroDAC = ui->label_FloatZero->text().toFloat();

hallDevice[0].ChannelSwitch(1);
QThread::msleep(500);
GetWriteInBufferValue();

StartChipCalibration(0);
QThread::msleep(120);
ChipRoughToWrite(0);
QThread::msleep(120);
ChipRoughWriteZeroL(0);
QThread::msleep(120);
ChipRoughWriteZeroH(0);
QThread::msleep(120);
ChipRoughWriteGainL(0);
QThread::msleep(120);
ChipRoughWriteGainH(0);
QThread::msleep(120);
returnValue[0].step=CaptureAfterRoughVoltage;
}

